﻿Imports System.Drawing
Imports TreeCreeperSpatial.fMain
Imports TreeCreeper01Interfaces
Imports System.Math

Public Class fMap

    Private mCanvas As Graphics
    Private mTranslation As cDeviceSpatialTranslation

    Private mRadiusPen As New Pen(Color.LightGray, 0)

    Private mPlantList As IPlantList
    'Private mColourList As cSpeciesColouringList
    Private mPlotExtent As cRectangleMetric

    Public Sub New(ByVal PlantList As IPlantList) ', ByVal ColourList As cSpeciesColouringList)

        InitializeComponent()
        mPlantList = PlantList
        'mColourList = ColourList
        EvaluateArea()

    End Sub

    Public Sub Display(ByVal Caption As String)

        Me.Text = Caption
        Show()

    End Sub

    Private Sub EvaluateArea()

        Dim MinimumX As Double
        Dim MaximumX As Double
        Dim MinimumY As Double
        Dim MaximumY As Double
        Dim LongestSide As Double
        Dim PlantIndex As Integer
        Dim RoundingMetreSize As Integer

        MinimumX = Double.MaxValue
        MinimumY = MinimumX
        MaximumX = Double.MinValue
        MaximumY = MaximumX

        With mPlantList
            For PlantIndex = 1 To .Count
                With .PlantByIndexBaseOne(PlantIndex)
                    MinimumX = Math.Min(MinimumX, .LocationCurrent.xCoordinate)
                    MinimumY = Math.Min(MinimumY, .LocationCurrent.yCoordinate)
                    MaximumX = Math.Max(MaximumX, .LocationCurrent.xCoordinate)
                    MaximumY = Math.Max(MaximumY, .LocationCurrent.yCoordinate)
                End With
            Next
        End With

        If MaximumY - MinimumY > MaximumX - MinimumX Then
            LongestSide = MaximumY - MinimumY
        Else
            LongestSide = MaximumX - MinimumX
        End If

        Select Case True
            Case LongestSide < 10
                RoundingMetreSize = 1
            Case LongestSide < 100
                RoundingMetreSize = 10
            Case Else
                RoundingMetreSize = 100
        End Select

        MinimumX = Math.Floor(MinimumX / RoundingMetreSize) * RoundingMetreSize
        MinimumY = Math.Floor(MinimumY / RoundingMetreSize) * RoundingMetreSize
        MaximumX = Math.Ceiling(MaximumX / RoundingMetreSize) * RoundingMetreSize
        MaximumY = Math.Ceiling(MaximumY / RoundingMetreSize) * RoundingMetreSize

        MinimumX = MinimumX - RoundingMetreSize
        MinimumY = MinimumY - RoundingMetreSize
        MaximumX = MaximumX + RoundingMetreSize
        MaximumY = MaximumY + RoundingMetreSize

        mPlotExtent = New cRectangleMetric((MinimumX), (MinimumY), (MaximumX), (MaximumY))

    End Sub

    Private Sub SetPictureSize()


        Dim HozRatio As Double
        Dim VerRatio As Double
        Dim PlotAspectRatio As Double
        Dim AspectRatio As Double

        PictureBoxMap.Left = 0
        PictureBoxMap.Top = 0

        PlotAspectRatio = mPlotExtent.Width / mPlotExtent.Height
        HozRatio = mPlotExtent.Width / ClientRectangle.Width
        VerRatio = mPlotExtent.Height / ClientRectangle.Height
        AspectRatio = HozRatio / VerRatio
        If AspectRatio > 1 Then
            PictureBoxMap.Width = ClientRectangle.Width
            PictureBoxMap.Height = CInt(PictureBoxMap.Width / PlotAspectRatio)
        Else
            PictureBoxMap.Height = ClientRectangle.Height
            PictureBoxMap.Width = CInt(PictureBoxMap.Height * PlotAspectRatio)
        End If

    End Sub

    Private Sub DrawMap()

        Dim RadiusMetric As Double
        Dim RadiusDevice As Integer
        Dim WorkingBitmap As Bitmap
        Dim Canvas As Graphics
        Dim PlantIndex As Integer
        'Dim SpeciesIndex As Integer
        Dim x As Double
        Dim y As Double
        Dim DotPen As New Pen(Color.Black, 0)
        Dim PlantBrush As New SolidBrush(Color.Black)


        SetPictureSize()


        With PictureBoxMap
            WorkingBitmap = New Bitmap(CInt(.Width), CInt(.Height))
            .Image = WorkingBitmap
            Canvas = Graphics.FromImage(.Image)
            mTranslation = New cDeviceSpatialTranslation(.Size, mPlotExtent)
        End With

        RadiusMetric = Me.UpdownRadius.Value
        RadiusDevice = mTranslation.TranslateRealWorldDistance(RadiusMetric)

        With mPlantList
            For PlantIndex = 1 To .Count
                With .PlantByIndexBaseOne(PlantIndex)

                    'SpeciesIndex = .TaxonomicSpecies.ShortLeafNodeId
                    PlantBrush.Color = .TaxonomicSpecies.DisplayColour
                    x = (.LocationCurrent.xCoordinate)
                    y = (.LocationCurrent.yCoordinate)
                End With
                With mTranslation.TranslateRealWorldCoordinate(x, y)
                    Canvas.FillEllipse(PlantBrush, New System.Drawing.Rectangle(.X - 4, .Y - 4, 8, 8))
                    WorkingBitmap.SetPixel(.X, .Y, Color.Black)
                    If Me.CheckDrawCircles.Checked Then
                        Canvas.DrawEllipse(mRadiusPen, New System.Drawing.Rectangle(.X - RadiusDevice, .Y - RadiusDevice, RadiusDevice * 2, RadiusDevice * 2))
                    End If
                End With
            Next
        End With

        Dim TickIndex As Double
        Dim StartPoint As Point
        Dim EndPoint As Point
        Dim Indent As Integer
        Indent = 10

        With mPlotExtent
            StartPoint = mTranslation.TranslateRealWorldCoordinate(0, 0)
            EndPoint = mTranslation.TranslateRealWorldCoordinate(100, 0)
            Canvas.DrawLine(mRadiusPen, StartPoint.X, StartPoint.Y, EndPoint.X, EndPoint.Y)
            EndPoint = mTranslation.TranslateRealWorldCoordinate(0, 100)
            Canvas.DrawLine(mRadiusPen, StartPoint.X, StartPoint.Y, EndPoint.X, EndPoint.Y)
        End With
        For TickIndex = 0 To 100 Step 10
            StartPoint = mTranslation.TranslateRealWorldCoordinate(TickIndex, 0)
            EndPoint = mTranslation.TranslateRealWorldCoordinate(TickIndex, -10)
            Canvas.DrawLine(mRadiusPen, StartPoint.X, StartPoint.Y, EndPoint.X, EndPoint.Y)
        Next
        For TickIndex = 0 To 100 Step 10
            StartPoint = mTranslation.TranslateRealWorldCoordinate(0, TickIndex)
            EndPoint = mTranslation.TranslateRealWorldCoordinate(-10, TickIndex)
            Canvas.DrawLine(mRadiusPen, StartPoint.X, StartPoint.Y, EndPoint.X, EndPoint.Y)
        Next


    End Sub

    Private Sub DrawOneCircle(ByVal x As Integer, ByVal y As Integer)

        Dim Canvas As Graphics
        Dim RadiusMetric As Double
        Dim RadiusDevice As Integer

        RadiusMetric = Me.UpdownRadius.Value
        RadiusDevice = mTranslation.TranslateRealWorldDistance(RadiusMetric)

        Canvas = Graphics.FromImage(PictureBoxMap.Image)
        Canvas.DrawEllipse(mRadiusPen, New System.Drawing.Rectangle(x - RadiusDevice, y - RadiusDevice, RadiusDevice * 2, RadiusDevice * 2))
        PictureBoxMap.Refresh()

    End Sub


    Private Sub fMap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DrawMap()
    End Sub

    Private Sub UpdownRadius_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdownRadius.ValueChanged
        If Me.Visible Then
            If Me.CheckDrawCircles.Checked Then
                DrawMap()
            End If
        End If
    End Sub

    Private Sub PictureBoxMap_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBoxMap.MouseClick

        If e.Button = Windows.Forms.MouseButtons.Right Then
            DrawOneCircle(e.X, e.Y)
        Else
        End If
    End Sub

    Private Sub PictureBoxMap_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBoxMap.MouseMove

        Dim RealX As Double
        Dim RealY As Double
        Dim Index As Integer
        Dim Limit As Double = 1
        Dim Info As String

        If e.Button = Windows.Forms.MouseButtons.Left Then

            Try
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
                PictureBoxMap.Enabled = False
                With mTranslation.TranslateDeviceCoordinate(e.X, e.Y)
                    RealX = .x
                    RealY = .y
                End With
                With mPlantList
                    For Index = 1 To .Count
                        With .PlantByIndexBaseOne(Index)
                            With .LocationCurrent
                                If Abs(.xCoordinate - RealX) < Limit Then
                                    If Abs(.yCoordinate - RealY) < Limit Then
                                        With mPlantList.PlantByIndexBaseOne(Index)
                                            Info = .ShortId.ToString
                                            Info &= "  " & .TaxonomicSpecies.Name
                                            With .LocationCurrent
                                                Info &= "  " & FormatNumber(.xCoordinate, 2, TriState.True, TriState.False) & " " & FormatNumber(.yCoordinate, 2, TriState.True, TriState.False)
                                            End With
                                            LabelReadoff.Text = Info
                                        End With
                                    End If
                                End If
                            End With
                        End With
                    Next
                End With
            Finally
                PictureBoxMap.Enabled = True
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            End Try
        Else
            With mTranslation.TranslateDeviceCoordinate(e.X, e.Y)
                LabelReadoff.Text = FormatNumber(.x, 2, TriState.True, TriState.False) & " " & FormatNumber(.y, 2, TriState.True, TriState.False)
            End With
        End If


    End Sub

    Private Sub fMap_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

        If Me.Visible Then DrawMap()

    End Sub

    Private Sub PictureBoxMap_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBoxMap.Resize

    End Sub

    Private Sub PictureBoxMap_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBoxMap.SizeChanged

    End Sub

    Private Sub LabelReadoff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelReadoff.Click

    End Sub

    Private Sub ToolTip_Popup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PopupEventArgs)

    End Sub

    Private Sub CheckDrawCircles_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckDrawCircles.CheckedChanged
        DrawMap()
    End Sub
End Class